/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.help.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.help.entity.SystemHelpOption;
import com.chlitina.store.modules.help.entity.SystemHelpOptionType;
import com.chlitina.store.modules.help.service.SystemHelpOptionService;
import com.chlitina.store.modules.help.service.SystemHelpOptionTypeService;

/**
 * 系统帮助页面实例Controller
 * @author lvbo
 * @version 2014-03-31
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/help/systemHelp")
public class SystemHelpOptionController extends BaseController {

	@Autowired
	private SystemHelpOptionService systemHelpOptionService;
	
	@Autowired
	private SystemHelpOptionTypeService systemHelpOptionTypeService;
	
	@ModelAttribute
	public SystemHelpOption get(@RequestParam(required=false) Long id) {
		if (id != null){
			return systemHelpOptionService.get(id);
		}else{
			return new SystemHelpOption();
		}
	}
	@RequestMapping(value = "videoHelp")
	public String videoHelp(SystemHelpOption systemHelpOption, HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/help/videoHelp";
	}
	@RequestMapping(value = "optionHelp")
	public String optionHelp(SystemHelpOption systemHelpOption, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SystemHelpOption> page = systemHelpOptionService.find(new Page<SystemHelpOption>(request, response), systemHelpOption);
		model.addAttribute("key", systemHelpOption.getSummary());
		model.addAttribute("page", page);
		return "modules/help/optionHelp";
	}
	@RequestMapping(value = "videoDetail")
	public String videoDetail(SystemHelpOption systemHelpOption, HttpServletRequest request, HttpServletResponse response, Model model) {
		String source = request.getParameter("source");
		model.addAttribute("source", source);
		return "modules/help/videoPlayer";
	}
	@RequestMapping(value = {"list", ""})
	public String list(SystemHelpOption systemHelpOption, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<SystemHelpOption> page = systemHelpOptionService.find(new Page<SystemHelpOption>(request, response), systemHelpOption); 
        model.addAttribute("page", page);
		return "modules/help/systemHelpOptionList";
	}

	@RequestMapping(value = "form")
	public String form(SystemHelpOption systemHelpOption, Model model) {
		Map<Long, String> optionTypeMap = systemHelpOptionTypeService.findAll();
		model.addAttribute("optionTypeMap", optionTypeMap);
		model.addAttribute("systemHelpOption", systemHelpOption);
		return "modules/help/systemHelpOptionForm";
	}

	@RequestMapping(value = "save")
	public String save(SystemHelpOption systemHelpOption, Model model, RedirectAttributes redirectAttributes) {
		if (systemHelpOption.getType() != null) {
			SystemHelpOptionType systemHelpOptionType = systemHelpOptionTypeService.get(systemHelpOption.getType().getId());
			systemHelpOption.setType(systemHelpOptionType);
		}
		if (!beanValidator(model, systemHelpOption)){
			return form(systemHelpOption, model);
		}
		systemHelpOptionService.save(systemHelpOption);
		addMessage(redirectAttributes, "保存系统帮助页面实例成功");
		return "redirect:"+Global.ADMIN_PATH+"/help/systemHelp/";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		systemHelpOptionService.delete(id);
		addMessage(redirectAttributes, "删除系统帮助页面实例成功");
		return "redirect:"+Global.ADMIN_PATH+"/help/systemHelp/?repage";
	}

}
